<template>
	<view>
		<view class="item">
			<view class="item-left">
				<view class="name">
					姓名
				</view>
			</view>
			<view class="item-right">
				<view class="input">
					<input type="text" v-model="name" placeholder="请填写用户姓名" />
				</view>
			</view>
		</view>
		<view class="item">
			<view class="item-left">
				<view class="name">
					手机号
				</view>
			</view>
			<view class="item-right">
				<view class="input">
					<input type="text" v-model="tel" placeholder="请填写手机号" />
				</view>
			</view>
		</view>
		<view class="item">
			<view class="item-left">
				<view class="name">
					省市区
				</view>
			</view>
			<view class="item-right">
				<picker mode="region" @change="cityChange">
					<view class="input">
						<input v-model="address" :disabled="true" type="text" placeholder="请选择省市区" />
					</view>
				</picker>
			</view>
		</view>
		<view class="item">
			<view class="item-left">
				<view class="name">
					详细地址
				</view>
			</view>
			<view class="item-right">
				<view class="input">
					<input type="text" v-model="xiang" placeholder="请填写详细地址" />
				</view>
			</view>
		</view>
		<view class="item">
			<view class="item-left">
				<view class="name">
					设为默认
				</view>
			</view>
			<view class="item-right">
				<view class="input">
					<u-switch @change="changeMO" v-model="mo" activeColor="#ef5350"></u-switch>
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="footer-btn btn1" @tap="DelAddress">
				删除地址
			</view>
			<view class="footer-btn btn2" @tap="edit">
				确认修改
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mo: false,
				name: '',
				tel: '',
				sheng: '',
				shi: '',
				qu: '',
				xiang: '',
				address: '',
				mos: 'fei',
				item: ''
			}
		},
		onLoad(options) {
			this.item = JSON.parse(options.item)
			console.log(this.item)
			this.name = this.item.name
			this.tel = this.item.tel
			this.sheng = this.item.sheng
			this.shi = this.item.shi
			this.qu = this.item.qu
			this.address = this.sheng + '-' + this.shi + '-' + this.qu
			this.xiang = this.item.xiang
			this.mos = this.item.mo
			if(this.mos == 'mo'){
				this.mo = true
			}else{
				this.mo = false
			}
		},
		methods: {
			DelAddress() {
				this.$http.Post('address/delete', {
					id: this.item.id
				}, (result) => {
					uni.showToast({
						title: '删除成功',
						icon: 'success'
					})
					setTimeout(res => {
						uni.navigateBack()
					}, 500)
				})
			},
			changeMO(e) {
				if (e) {
					this.mos = 'mo'
				} else {
					this.mos = 'fei'
				}
			},
			cityChange(e) {
				console.log(e)
				this.address = e.detail.value[0] + '-' + e.detail.value[1] + '-' + e.detail.value[2]
				this.sheng = e.detail.value[0]
				this.shi = e.detail.value[1]
				this.qu = e.detail.value[2]
			},
			edit() {
				if (!this.name || !this.tel || !this.sheng || !this.shi || !this.qu || !this.xiang) {
					uni.showToast({
						title: "请填写相关信息",
						icon: 'none'
					})
					return
				}
				this.$http.Post('address/edit', {
					id: this.item.id,
					sheng: this.sheng,
					shi: this.shi,
					qu: this.qu,
					name: this.name,
					tel: this.tel,
					xiang: this.xiang,
					mo: this.mos
				}, (result) => {
					uni.showToast({
						title: '修改成功',
						icon: 'success'
					})
					setTimeout(res => {
						uni.navigateBack()
					}, 500)
				})
			}
		}
	}
</script>

<style>
	.item {
		width: 94%;
		margin: 20rpx auto;
		background-color: #fff;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-radius: 8rpx;
	}

	.name {
		padding-left: 20rpx;
	}

	.input>input {
		text-align: right;
		padding-right: 20rpx;
	}

	.item-left {
		font-size: 28rpx;
		color: #3c3c3c;
	}

	.item-right {
		font-size: 28rpx;
	}

	.footer {
		position: fixed;
		bottom: 80rpx;
		height: 100rpx;
		width: 94%;
		margin-left: 3%;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.footer-btn {

		width: 49%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 999rpx;
	}

	.btn1 {
		color: #ef5350;
		font-size: 28rpx;
		border: 2rpx solid #ef5350;
	}

	.btn2 {
		color: #fff;
		font-size: 28rpx;
		background-color: #ef5350;
	}
</style>